import axios from 'axios'
import apis from '../api/api'

const staticRoutes = [
    {
        path:'/',
        name:'entrance',
        redirect:'/container/home'
    },
    {
        path:'/container',
        name:'container',
        meta:{index:0},
        component: resolve => require(['../components/pages/container/container'], resolve),
        children:[
            {
                path:'/container/home',
                name:'home',
                meta:{index:1},
                component: resolve => require(['../components/pages/home/home'], resolve),
                beforeEnter(to, from, next){
                    let temUrl = window.location.href;
                    let token = localStorage.getItem('token');
                    let regDis = /distributor_id=([a-zA-Z0-9_]+)&/;
                    let distributionUrl = temUrl+'&';
                    let disId = '';
                    distributionUrl.replace(regDis, function($0,$1){
                        disId = $1;
                    })
                    if(disId){
                        localStorage.setItem('distribution', disId);
                    }
                    if(token){
                        next();
                    }else{
                        let regCode = /code=/;
                        alert(regCode.test(temUrl));
                        if(!regCode.test(temUrl)){
                            next('/auth');
                        }else{
                           next(); 
                        }
                    }
                }
                // beforeEnter(to, from, next){
                //     let token = localStorage.getItem('token');
                //     let temUrl = window.location.href;
                //     alert('route === '+temUrl);
                //     alert('token before in ==='+token);
                //     // 判断用户是否通过二维码进入首页
                //     let regDis = /distributor_id=([a-zA-Z0-9_]+)&/;
                //     let disId = '';
                //     let distributionUrl = temUrl+'&';
                //     distributionUrl.replace(regDis, function($0,$1){
                //         disId = $1;
                //     })
                //     if(disId){
                //         localStorage.setItem('distribution', disId);
                //     }

                //     if(!token){
                //         alert('start auth');
                //         alert('token after === '+token);
                //         axios.post(apis.userCode, {
                //             'url':temUrl
                //         }).then(function(res){
                //             window.location.href = res.data.msg;
                //             sessionStorage.setItem('temUrl', window.location.href+'&');
                //         }).catch(function(error){
                //             alert(err);
                //         })
                //         next();
                //     }else{
                //         alert('logined');
                //         next();
                //     }
                // }
            },
            {
                path:'/container/classify',
                name:'classify',
                meta:{index:2},
                component: resolve => require(['../components/pages/classify/classify'], resolve),
                beforeEnter(to, from, next){
                    window.scrollTo(0, 0);
                    next();
                }
            },
            {
                path:'/container/classify_detail',
                name:'classifyDetail',
                meta:{isChild:true},
                component: resolve => require(['../components/pages/classify/classifyDetail'], resolve),
                beforeEnter(to, from, next){
                    window.scrollTo(0, 0);
                    next();
                }
            },
            {
                path:'/container/credits',
                name:'credits',
                meta:{index:3},
                component: resolve => require(['../components/pages/credits/credits'], resolve)
            },
            {
                path:'/container/shopping',
                name:'shopping',
                meta:{index:4},
                component: resolve => require(['../components/pages/shopping/shopping'], resolve),
                beforeEnter(to, from, next){
                    window.scrollTo(0, 0);
                    next();
                }
            },
            {
                path:'/container/me',
                name:'me',
                meta:{index:5},
                component: resolve => require(['../components/pages/me/me'], resolve)
            }
        ]
    },
    {
        path:'/product',
        name:'product',
        meta:{index:6, isChild:true},
        component: resolve => require(['../components/pages/product/product'], resolve),
        beforeEnter(to,from,next){
            window.scrollTo(0,0);
            next();
        }
    },
    {
        path:'/order',
        name:'order',
        meta:{index:7, isChild:true},
        component: resolve => require(['../components/pages/order/order'], resolve)
    },
    {
        path:'/distribution',
        name:'distribution',
        component:resolve => require(['../components/pages/distribution/distribution'], resolve),
        redirect:'/distribution/center',
        children:[
            {
                path:"/distribution/center",
                name:'center',
                meta:{index:9, isChild:true},
                component:resolve => require(['../components/pages/distribution/center'], resolve),
            },
            {
                path:'/distribution/codeer',
                name:'codeer',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/code'], resolve),
            },
            {
                path:'/distribution/team',
                name:'team',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/team'], resolve),
            },
            {
                path:'/distribution/team_lv3',
                name:'teamLv3',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/teamLv3'], resolve),
            },
            {
                path:'/distribution/commission',
                name:'commission',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/commission'], resolve),
            },
            {
                path:'/distribution/commission_detail',
                name:'commissionDetail',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/commissionDetail'], resolve),
            },
            {
                path:'/distribution/commission_detail_msg',
                name:'commissionDetailMsg',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/commissionMsg'], resolve),
            },
            {
                path:'/distribution/money',
                name:'money',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/money'], resolve),
            },
            {
                path:'/distribution/money_msg',
                name:'moneyMsg',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/moneyMsg'], resolve),
            },
            {
                path:'/distribution/notice',
                name:'notice',
                meta:{index:10, isChild:true},
                component:resolve => require(['../components/pages/distribution/notice'], resolve),
            }
        ]
    },
    {
        path:'/address',
        name:'address',
        meta:{index:7, isChild:true},
        component:resolve => require(['../components/pages/address/address'], resolve),
        redirect:'/address/list',
        children:[
            {
                path:'/address/list',
                name:'addressList',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/address/addressList'], resolve)
            },
            {
                path:'/address/add',
                name:'addressAdd',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/address/addAddress'], resolve)
            },
            {
                path:'/address/edit',
                name:'addressEdit',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/address/editAddress'], resolve)
            }
        ]
    },
    {
        path:'/members',
        name:'members',
        meta:{isChild:true},
        component:resolve => require(['../components/pages/member/member'], resolve),
        redirect:'/members/privilege',
        children:[
            {
                path:'/members/privilege',
                name:'privilege',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/member/privilege'], resolve)
            },
            {
                path:'/members/rights',
                name:'rights',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/member/rights'], resolve),
                beforeEnter(to, from, next){
                    window.scrollTo(0, 0);
                    next();
                }
            },
            {
                path:'/members/rules',
                name:'rules',
                meta:{isChild:true},
                component:resolve => require(['../components/pages/member/rules'], resolve),
                beforeEnter(to, from, next){
                    window.scrollTo(0, 0);
                    next();
                }
            },
        ]
    },
    {
        path:'/collect',
        name:'collect',
        meta:{isChild: true},
        component: resolve => require(['../components/pages/collect/collect'], resolve)
    },
    {
        path:'/ticket',
        name:'ticket',
        meta:{isChild:true},
        component:resolve => require(['../components/pages/ticket/ticket'], resolve)
    },
    {
        path:'/order/wait_payment',
        name:'waitPayment',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/waitPayment'], resolve)
    },
    {
        path:'/order/wait_deliver',
        name:'waitDeliver',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/waitDeliver'], resolve)
    },
    {
        path:'/order/wait_goods',
        name:'waitGoods',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/waitGoods'], resolve)
    },
    {
        path:'/order/wait_comment',
        name:'waitComment',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/waitComment'], resolve)
    },
    {
        path:'/order/refund',
        name:'refund',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/refund'], resolve)
    },
    {
        path:'/comment',
        name:'comment',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/comment/comment'], resolve)
    },
    {
        path:'/pay',
        name:'pay',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/pay/pay'], resolve)
    },
    {
        path:'/pay/payed',
        name:'payed',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/pay/pay'], resolve)
    },
    {
        path:'/search',
        name:'search',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/search/search'], resolve)
    },
    {
        path:'/egg',
        name:'egg',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/activity/egg'], resolve)
    },
    {
        path:'/express',
        name:'express',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/express/express'], resolve)
    },
    {
        path:'/nine',
        name:'nine',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/activity/nine'], resolve)
    },
    {
        path:'/order_detail',
        name:'orderDetail',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/order/detail'], resolve)
    },
    {
        path:'/comment_detail',
        name:'commentDetail',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/comment/commentDetail'], resolve)
    },
    {
        path:'/activity_card',
        name:'activityCard',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/ticket/activityCard'], resolve)
    },
    {
        path:'/create_order',
        name:'createOrder',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/product/createOrder'], resolve)
    },
    {
        path:'/create_order_credits',
        name:'createOrderCredits',
        meta:{isChild:true},
        component: resolve => require(['../components/pages/product/creditsCreateOrder'], resolve)
    },
    {
        path:'/auth',
        name:'auth',
        meta:{isChild:false},
        component: resolve => require(['../components/pages/auth/auth'], resolve)
    }
]

export default staticRoutes;